package com.clever.mall.controller;

import com.clever.mall.commons.dto.ConsignmentBaseInfoRequestParam;
import com.clever.mall.commons.dto.ConsignmentRequestParam;
import com.clever.mall.commons.dto.UpdateAllPriceRequestParam;
import com.clever.mall.commons.dto.UserRequestParam;
import com.clever.mall.commons.exception.DatabaseException;
import com.clever.mall.commons.exception.ServiceException;
import com.clever.mall.commons.result.BaseResult;
import com.clever.mall.commons.result.ConsignmentResult;
import com.clever.mall.commons.result.TotalConsignmentResult;
import com.clever.mall.service.SellService;
import com.clever.mall.utils.StatusCode;
import com.clever.mall.vo.ProductCheckVo;
import com.clever.mall.vo.ProductVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author ChenWang
 * @className SellController
 * @date 2020/11/11 09:30
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/sell")
public class ConsignmentController {
    @Resource
    SellService sellService;
    /**
     * 获取我的在售商品信息
     *
     * @param userRequestParam 指定的个人信息
     * @return  个人在售商品信息
     */
    @PostMapping("/onSale")
    public BaseResult<List<ProductVo>> onSale(@Validated @RequestBody UserRequestParam userRequestParam,
                                                @RequestHeader(value = "page",required = false, defaultValue = "0") int page,
                                                @RequestHeader(value = "size",required = false, defaultValue = "6") int size) {
        List<ProductVo> productVos = null;
        productVos = sellService.getOnSaleProductVos(userRequestParam, page, size);
        return BaseResult.success(productVos);
    }
    /**
     * 获取我的在售商品信息
     *
     * @param userRequestParam 指定的个人信息
     * @return  个人在售商品信息
     */
    @PostMapping("/beenSold")
    public BaseResult<List<ProductVo>> beenSold(@Validated @RequestBody UserRequestParam userRequestParam,
                                              @RequestHeader(value = "page",required = false, defaultValue = "0") int page,
                                              @RequestHeader(value = "size",required = false, defaultValue = "6") int size) {
        List<ProductVo> productVos = null;
        productVos = sellService.getBeenSoldProductVos(userRequestParam, page, size);
        return BaseResult.success(productVos);
    }
    /**
     * 提交订单
     *
     * @param consignmentBaseInfoRequestParam 指定的订单信息
     * @return 个人主页信息
     */
    @PostMapping("/submitConsignment")
    public BaseResult<Integer> submitConsignment(@Validated @RequestBody ConsignmentBaseInfoRequestParam consignmentBaseInfoRequestParam
    ) {
        int flag = 0;
        try {
            flag = sellService.saveConsignment(consignmentBaseInfoRequestParam);
        } catch (Exception e) {
            throw new ServiceException(StatusCode.SERVER_ERROR);
        }
        return BaseResult.success(flag);
    }

    /**
     * 展示指定用户的所有的相关审核的寄卖单的信息
     *
     * @param userRequestParam  指定的用户信息
     * @return                  指定的订单集合
     */
    @PostMapping("/check")
    public BaseResult<TotalConsignmentResult> check(@Validated @RequestBody UserRequestParam userRequestParam) {
        TotalConsignmentResult totalConsignmentResult = null;
        try {
            totalConsignmentResult = sellService.getTotalCheckResult(userRequestParam);
            if (Objects.isNull(totalConsignmentResult)) {
                throw new ServiceException(StatusCode.SERVICE_ERROR);
            }
        } catch (Exception e) {
            throw new ServiceException(StatusCode.SERVER_ERROR);
        }
        return BaseResult.success(totalConsignmentResult);
    }
    /**
     * 展示指定用户的所有的相关审核的寄卖单的信息
     *
     * @param userRequestParam  指定的用户信息
     * @return                  指定的订单集合
     */
    @PostMapping("/returnList")
    public BaseResult<TotalConsignmentResult> returnList(@Validated @RequestBody UserRequestParam userRequestParam) {
        TotalConsignmentResult totalConsignmentResult = null;
        try {
            totalConsignmentResult = sellService.getTotalReturnResult(userRequestParam);
            if (Objects.isNull(totalConsignmentResult)) {
                throw new ServiceException(StatusCode.SERVICE_ERROR);
            }
        } catch (Exception e) {
            throw new ServiceException(StatusCode.SERVER_ERROR);
        }
        return BaseResult.success(totalConsignmentResult);
    }

    /**
     * 查询指定的寄送单的未定价的商品
     *
     * @param consignmentRequestParam 指定的定价单
     * @return 指定的寄送单的未定价的商品的封装数据包
     */
    @PostMapping("/entryIdentifyPrice")
    public BaseResult<List<ProductCheckVo>> entryIdentifyPrice(@Validated @RequestBody ConsignmentRequestParam consignmentRequestParam) {
        List<ProductCheckVo> productCheckVos = null;
        productCheckVos = sellService.getProductCheckVos(consignmentRequestParam);
        if (Objects.isNull(productCheckVos)) {
            throw new ServiceException(StatusCode.CONSIGNMENT_DATA_READ_ERROR);
        }
        return BaseResult.success(productCheckVos);
    }

    @PostMapping("/entryReturn")
    public BaseResult<ConsignmentResult> entryReturn(@Validated @RequestBody ConsignmentRequestParam consignmentRequestParam) {
        ConsignmentResult consignmentResult = null;
        try {
            consignmentResult = sellService.getConsignmentUnPassResult(consignmentRequestParam);
            if (Objects.isNull(consignmentResult)) {
                throw new ServiceException(StatusCode.SERVICE_ERROR);
            }
        } catch (Exception e) {
            throw new ServiceException(StatusCode.SERVER_ERROR);
        }
        return BaseResult.success(consignmentResult);
    }

    /**
     * 一次更新多个商品的价格信息
     *
     * @param updateAllPriceRequestParam 指定商品的更新信息
     * @return 0表示失败, 大于0表示成功--封装在数据包中
     */
    @PostMapping("/updatePrice")
    public BaseResult<Integer> updatePrice(@Validated @RequestBody UpdateAllPriceRequestParam updateAllPriceRequestParam) {
        int flag = 0;
        try {
            flag = sellService.updateAllPrice(updateAllPriceRequestParam);
        } catch (Exception ex) {
            throw new DatabaseException(StatusCode.DATABASE_UPDATE_PRICE_ERROR);
        }
        return BaseResult.success(flag);
    }

    /**
     * 确认退回指定寄卖单审核未通过的商品
     *
     * @param consignmentRequestParam 指定寄卖单
     * @return
     */
    @PostMapping("/identifyReturn")
    public BaseResult<ConsignmentResult> identifyReturn(@Validated @RequestBody ConsignmentRequestParam consignmentRequestParam) {
        ConsignmentResult consignmentResult = null;
        try {
            consignmentResult = sellService.getConsignmentUnPassResult(consignmentRequestParam);
            sellService.identifyReturn(consignmentRequestParam);
            if (Objects.isNull(consignmentResult)) {
                throw new ServiceException(StatusCode.SERVICE_ERROR);
            }
        } catch (Exception ex) {
            throw new DatabaseException(StatusCode.DATABASE_UPDATE_PRICE_ERROR);
        }
        return BaseResult.success(consignmentResult);
    }

    /**
     * 确认不要了指定寄卖单审核未通过的商品
     *
     * @param consignmentRequestParam 指定寄卖单
     * @return
     */
    @PostMapping("/identifyAbandon")
    public BaseResult<ConsignmentResult> identifyAbandon(@Validated @RequestBody ConsignmentRequestParam consignmentRequestParam) {
        ConsignmentResult consignmentResult = null;
        try {
            consignmentResult = sellService.getConsignmentUnPassResult(consignmentRequestParam);
            sellService.identifyAbandon(consignmentRequestParam);
            if (Objects.isNull(consignmentResult)) {
                throw new ServiceException(StatusCode.SERVICE_ERROR);
            }
        } catch (Exception ex) {
            throw new DatabaseException(StatusCode.DATABASE_UPDATE_PRICE_ERROR);
        }
        return BaseResult.success(consignmentResult);
    }

}
